﻿using System.Windows.Forms;
using DesignerHelper.Addons;
using SpreadsheetGear;

namespace WorkbookDesigner.Addons.WorkbookExplorer
{
    public partial class Header : UserControl, IScv<IPageSetup>
    {
        public Header()
        {
            InitializeComponent();
        }

        public Header(IPageSetup ps) : this()
        {
            TriggerInit(ps);
        }

        private IPageSetup PageSetup { get; set; }

        private string CHeader
        {
            get { return textBoxCenterHeader.Text; }
            set { textBoxCenterHeader.Text = value; }
        }

        private string LHeader
        {
            get { return textBoxLeftHeader.Text; }
            set { textBoxLeftHeader.Text = value; }
        }

        private string RHeader
        {
            get { return textBoxRightHeader.Text; }
            set { textBoxRightHeader.Text = value; }
        }

        public void TriggerInit(IPageSetup ps)
        {
            PageSetup = ps;
            Init();
        }

        public void TriggerSet()
        {
            Set();
        }

        public void TriggerReset()
        {
            Init();
        }

        private void Init()
        {
            IPageSetup ps = PageSetup;
            CHeader = ps.CenterHeader;
            LHeader = ps.LeftHeader;
            RHeader = ps.RightHeader;
        }

        private void Set()
        {
            IPageSetup ps = PageSetup;
            ps.CenterHeader = CHeader;
            ps.LeftHeader = LHeader;
            ps.RightHeader = RHeader;
        }

        private void panelAllText_Resize(object sender, System.EventArgs e)
        {
            int h = (this.Height
                     - panelLeftHeader.Margin.Top - panelLeftHeader.Margin.Bottom
                     - panelCenterHeader.Margin.Top - panelCenterHeader.Margin.Bottom
                     - panelRightHeader.Margin.Top - panelRightHeader.Margin.Bottom)/3;

            panelRightHeader.Height = panelCenterHeader.Height = panelLeftHeader.Height = h;

            panelCenterHeader.Location = new System.Drawing.Point(panelCenterHeader.Margin.Left,
                Height - panelLeftHeader.Margin.Top - h
                - panelLeftHeader.Margin.Bottom - panelCenterHeader.Margin.Top);

            panelRightHeader.Location = new System.Drawing.Point(panelRightHeader.Margin.Left,
                panelCenterHeader.Location.Y - h
                - panelCenterHeader.Margin.Bottom - panelRightHeader.Margin.Top);
        }

        #region interface

        void IScv<IPageSetup>.TriggerReset()
        {
            TriggerReset();
        }

        void IScv<IPageSetup>.TriggerSet()
        {
            TriggerSet();
        }

        Control IScv<IPageSetup>.GetControl()
        {
            return this;
        }

        #endregion
    }
}